package edu.ptu.acl.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.ptu.acl.common.ResultVO;
import edu.ptu.acl.dto.param.DeptParam;
import edu.ptu.acl.entity.User;
import edu.ptu.acl.exception.ApiException;
import edu.ptu.acl.service.IDeptService;
import edu.ptu.acl.service.IUserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.Min;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LinJC
 * @since 2022-03-15
 */
@RestController
@RequestMapping("/dept")
@Validated
public class DeptController {

    @Resource
    private IDeptService deptService;
    @Resource
    private IUserService userService;

    @GetMapping("/getDeptTree")
    public ResultVO getDeptTree(){
        return ResultVO.success(deptService.getDeptTree());
    }

    @GetMapping("/getDeptUser/{deptId}/{pageNo}/{pageSize}")
    public ResultVO getDeptUser(@PathVariable  @Min(value = 10000000000000000L) Long deptId,
                                @PathVariable @Min(value = 1) Integer pageNo,
                                @PathVariable @Min(value = 1) Integer pageSize){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dept_id",deptId);
        Page<User> page = new Page<>(pageNo,pageSize);
        userService.page(page,queryWrapper);

        return ResultVO.success(page);
    }


    @PostMapping("/saveDept")
    public ResultVO saveDept(@RequestBody @Validated(DeptParam.Save.class) DeptParam deptParam){
        deptService.saveDept(deptParam);
        return ResultVO.success();
    }

    @PutMapping("/updateDept")
    public ResultVO updateDept(@RequestBody @Validated(DeptParam.Update.class) DeptParam deptParam){
        deptService.updateDept(deptParam);
        return ResultVO.success();
    }

    @DeleteMapping("/deleteDept/{id}")
    public ResultVO deleteDept(@PathVariable("id") @Min(value = 10000000000000000L) Long deptId){
        deptService.deleteDept(deptId);
        return ResultVO.success();
    }



}
